<?php

/**
 * Created by PhpStorm.
 * User: 子不语
 * Date: 2016/7/29
 * Time: 14:30
 */
class ExtensionAction extends BaseAction
{
    function index(){
        $ajax=Req::get('ajax')?1:0;
        if($ajax)return $this->ajax_extension_list($ajax);
        $this->ajax_extension_list($ajax);
        $this->display('extension_index');
    }
    function firends(){
        $this->display('extension_firends');
    }
    function money(){
        $g=Req::get('g','intval')?:0;
        $sql = 'select sum(s.tzpoints) as point from game_day_static s left join users u on u.id=s.uid where u.tjid=' . $_SESSION['usersid'] . ' and to_days(now())-to_days(s.time)=1';
        $res = db::get_one($sql,'assoc');
        $point = $res['point'];
        if ($point > 0) {
            $point *= 0.001;
        }else{
            $point= 0;
        }
        $point=intval($point);
        $sql='select id from score_log where opr_type=21 and uid='.$_SESSION['usersid'].' and to_days(now())=to_days(log_time)';
        $res=db::get_one($sql,'assoc');
        if($res['id']){
            $this->assign('id',$res['id']);
        }elseif($g && $point>0){
            db::_query('begin');
            //$db->query('SET AUTOCOMMIT=0');
            $sql='update users set `points`=`points`+'.$point.' where id='.$_SESSION['usersid'];
            if(!db::_query($sql)){
                db::_query('rollback');
                return result(1, '失败了,请联系管理员');
            }
            $this->update_centerbank($point,7);
            $sql = "INSERT game_static (uid,typeid,points) values (" . $_SESSION['usersid'] . ",104, $point) ON DUPLICATE KEY UPDATE points=points+'$point'";
            db::_query($sql);
            $sql = "INSERT webtj (time,tgpoints) values (now(),$point) ON DUPLICATE KEY UPDATE tgpoints=tgpoints+'$point'";
            db::_query($sql);
            $this->RefreshPoints();
            $this->score_log($_SESSION['usersid'],21,$point,$_SESSION['points'],$_SESSION['bankpoints'],'推广返利');

            db::_query('commit');
            db::_query('SET AUTOCOMMIT=1');
            //RefreshPoints();
            return $this->result(0, '领取成功');
        }
        $this->assign('point',floor(abs($point)));
        $sql='select count(*) as total from users where tjid='.$_SESSION['usersid'];
        $count=db::get_one($sql);
        $this->assign('count',$count->total);
        $sql='select amount,log_time from score_log where opr_type=21 and uid='.$_SESSION['usersid'].' order by id desc limit 30';
        $list=db::get_all($sql);
        $this->assign('list',$list);
        $this->display('extension_get');
    }
    function ajax_extension_list($ajax){
        $page=Req::get('page','intval')?:1;

        $sql='select count(id) as total from users where tjid='.$_SESSION['usersid'];
        $total=db::get_one($sql);
        $pagesize=10;
        $sql='select u.id,u.nickname,u.logintime,ifnull(g.tzpoints,0) as tzpoints from users u left join game_day_static g on g.uid=u.id and (TO_DAYS(now())-to_days(g.time)<=1) where u.tjid='.$_SESSION['usersid'].' limit '.(($page-1)*$pagesize).',10';
        $user_list=db::get_all($sql);//u.tjid='.$_SESSION['usersid'].'
        $ajaxpage=new Apage(array('total'=>$total->total,'perpage'=>$pagesize,'ajax'=>"page_list",'nowindex' => $page));
        $show= $ajaxpage->show();
        if($ajax){
            echo json_encode(['list'=>$user_list,'page'=>$show],JSON_UNESCAPED_UNICODE);
            return true;
        }
        $this->assign('user_list',$user_list);
        $this->assign('page',$show);
    }
}